package com.example.myapplication.adapter.recyclerViewAdapter;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

import com.bumptech.glide.Glide;
import com.bumptech.glide.load.engine.DiskCacheStrategy;
import com.bumptech.glide.load.resource.bitmap.RoundedCorners;
import com.bumptech.glide.request.RequestOptions;
import com.example.myapplication.activity.PlayListDetailActivity;
import com.example.myapplication.R;
import com.example.myapplication.entity.RecommendResource;
import com.example.myapplication.tools.SetColor;

import java.util.List;

public class MusicHallHorAdapter extends RecyclerView.Adapter<MusicHallHorAdapter.MusicHallHorHolder>  {
    private List<RecommendResource.RecommendBean> recommendBeanList;
    private Context context;

    public void setData(List<RecommendResource.RecommendBean> recommendBeanList){
        this.recommendBeanList=recommendBeanList;
        notifyDataSetChanged();
    }

    public MusicHallHorAdapter(Context context) {
        this.context = context;
    }

    @NonNull
    @Override
    public MusicHallHorHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.item_music_hall,parent,false);
        return new MusicHallHorHolder(view);
    }

    @Override
    public void onBindViewHolder(@NonNull MusicHallHorHolder holder, int position) {
        if(recommendBeanList!=null) {
            RecommendResource.RecommendBean recommendBean = recommendBeanList.get(position);
            holder.tv_music_name.setText(recommendBean.getName());
            holder.play_volume_hall.setText(recommendBean.getCreator().getNickname());
            RoundedCorners roundedCorners = new RoundedCorners(30);
            RequestOptions options = RequestOptions.bitmapTransform(roundedCorners).override(300, 300);
            Glide.with(context).load(recommendBean.getPicUrl())
                    .centerCrop()
                    .diskCacheStrategy(DiskCacheStrategy.ALL)
                    .error(R.drawable.ic_baseline_refresh_24)
                    .placeholder(R.drawable.ic_baseline_refresh_24)
                    .apply(options)
                    .into(holder.img_hall);
            SetColor.asBitmap(recommendBean.getPicUrl(),context,holder.play_volume_hall);

            holder.view.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    Intent intent = new Intent(context, PlayListDetailActivity.class);
                    Bundle bundle = new Bundle();
                    bundle.putString("playList",recommendBean.getId());
                    intent.putExtras(bundle);
                    context.startActivity(intent);
                }
            });
        }
    }

    @Override
    public int getItemCount() {
        return recommendBeanList == null?0:recommendBeanList.size();
    }


    class MusicHallHorHolder extends RecyclerView.ViewHolder{
        TextView tv_music_name;
        ImageView img_hall;
        TextView play_volume_hall;
        View view;
        public MusicHallHorHolder(@NonNull View itemView) {
            super(itemView);
            tv_music_name = itemView.findViewById(R.id.tv_music_name);
            img_hall = itemView.findViewById(R.id.img_hall);
            play_volume_hall = itemView.findViewById(R.id.play_volume_hall);
            view = itemView.findViewById(R.id.view);
        }
    }
}
